#!/bin/sh

# Set up dm-verity for root filesystem

set -e

prereqs() {
    echo ""
}

case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac

# Without this, error exit codes are ignored and the boot continues
trap 'panic "fatal: verity-root failed"' ERR

. /scripts/functions

echo "Original root: ${ROOT}"

# Obtain root hash from kernel commandline
for ARG in $(cat /proc/cmdline); do
    VAL=${ARG#root_hash=}
    if [ ! "${VAL}" = "${ARG}" ]; then
        ROOT_HASH="${VAL}"
    fi
done

echo "Root hash: ${ROOT_HASH}"

veritysetup open "${ROOT}" vroot "${ROOT}" "${ROOT_HASH}" --hash-offset 10603200512

echo "ROOT=/dev/mapper/vroot" >/conf/param.conf

exit 0
